<?php

namespace app\common\model\Merchant;
use app\common\model\member\GroupModel;
use app\common\model\member\MemberModel;
use app\common\model\promotion\PromotionUserModel;
use think\Model;

class MerchantModel extends Model
{
    // 表名
    protected $name = 'merchant';
    // 表主键
    protected $pk = 'id';

    // 关联推广员
    public function promotion()
    {
        return $this->hasOne(PromotionUserModel::class, 'promotion_user_id', 'promotion_user_id');
    }
    // 关联上级
    public function parentMer()
    {
        return $this->belongsTo(MerchantModel::class, 'merchant_pid', 'id');
    }


    // 关联分组
    public function groups()
    {
        return $this->belongsToMany(GroupModel::class, MerchantAttributesModel::class, 'group_id', 'mer_id');
    }
    /**
     * 获取分组id
     * @Apidoc\Field("")
     * @Apidoc\AddField("group_ids", type="array", desc="分组id")
     */
    public function getGroupIdsAttr()
    {
        return relation_fields($this['groups'], 'group_id');
    }
    /**
     * 获取分组名称
     * @Apidoc\Field("")
     * @Apidoc\AddField("group_names", type="string", desc="分组名称")
     */
    public function getGroupNamesAttr()
    {
        return relation_fields($this['groups'], 'group_name', true);
    }
}
